﻿using UnityEngine;
using System.Collections;

public class TurnGame : MonoBehaviour {

    public static TurnGame _instance;

    public OperatorState mState = OperatorState.PlayerRound;

    public GameObject eventSystemGO;

    public int Round = 1;

    public GameObject EndGameView;

    void Awake()
    {
        _instance = this;
    }

	// Update is called once per frame
	void Start () {
        Invoke("GameControl", 1);
	}

    public void GameControl()
    {
        if (mState == OperatorState.PlayerQuit)
        {
            HealthInfoView._instance.UpdateHealth();
            eventSystemGO.gameObject.SetActive(true);
            EndGameView.SetActive(true);
            GameOver();
            Debug.Log("玩家输了");
        }
        else if (mState == OperatorState.EnemyQuit)
        {
            HealthInfoView._instance.UpdateHealth();
            eventSystemGO.gameObject.SetActive(true);
            EndGameView.SetActive(true);
            GameOver();
            Debug.Log("玩家赢了");
        }
        else
        {
            switch (mState)
            {
                case OperatorState.PlayerRound:
                    HealthInfoView._instance.UpdateHealth();
                    eventSystemGO.SetActive(true);
                    GameMessage._instance.ShowText("第 " + Round + "回合\n玩家行动");
                    MapManager._instance.ClearMapGridState();
                    PlayerAttack._instance.AssignAttackFood();
                    MapManager._instance.ShowRange(TeamManager._instance.PlayerFoods[PlayerAttack._instance.MainAttack]);
                    //MapManager._instance.SetMapGridRed(TeamManager._instance.PlayerFoods[PlayerAttack._instance.MainAttack]);
                    //mState = OperatorState.EnemyRound;
                    break;
                case OperatorState.EnemyRound:
                    HealthInfoView._instance.UpdateHealth();
                    MapManager._instance.ClearMapGridState();
                    eventSystemGO.SetActive(false);
                    EnemyAI._instance.RandomPos();
                    GameMessage._instance.ShowText("第 " + Round + "回合\n敌人行动");
                    EnemyAI._instance.PlayEffect();
                    Round++;
                    //mState = OperatorState.PlayerRound;
                    break;
            }
        }
    }

    private void GameOver()
    {
        int i = 0;
        float f = TeamManager._instance.CurrenPlayerHealth / TeamManager._instance.MaxPlayerHealth;
        if (mState == OperatorState.PlayerQuit)
        {
            i = 3;
            EndGameView.GetComponent<EndGame>().Init("您输了", i);
        }
        if (mState == OperatorState.EnemyQuit)
        {
            if (f < 0.3f)
                i = 1;
            else if (f < 0.6f)
                i = 2;
            else
                i = 3;

            EndGameView.GetComponent<EndGame>().Init("您赢了", i);
        }
    }
}

public enum OperatorState
{
    PlayerQuit,
    EnemyRound,
    PlayerRound,
    EnemyQuit
}